package cn.stard.org.service.impl;


import cn.stard.basic.service.impl.BaseServiceImpl;
import cn.stard.org.domain.DePartment;

import cn.stard.org.mapper.DePartmentMapper;
import cn.stard.org.service.IDepartmentservice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Service
public class DepartmentserviceImpl extends BaseServiceImpl<DePartment> implements IDepartmentservice {

    @Autowired
    private DePartmentMapper dePartmentMapper;



    @Override
    @Transactional
    public void save(DePartment department) {
        dePartmentMapper.save(department);
        // 给dirPath拼接值
        String dirPath = "";
        if (department.getParent() == null){
            // 如果parent对象为Null,说明他是顶级对象，那么dirPath就是他自己的主键id
            dirPath = "/" + department.getId();
        }else {
            // 否则他就有上级部门，那么我们需要查询到上级部门的对象，获取到上级部门的dirpath，拼接我自己的id，就是我的dirpath
            DePartment parentDept = dePartmentMapper.loadById(department.getParent().getId());
            if(parentDept != null){
                dirPath = parentDept.getDirPath() + "/" + department.getId();
            }
        }
        department.setDirPath(dirPath);
        dePartmentMapper.update(department);
    }


    @Override
    @Transactional
    public void update(DePartment department) {
        // 给dirPath拼接值
        String dirPath = "";
        if (department.getParent() == null){
            // 如果parent对象为Null,说明他是顶级对象，那么dirPath就是他自己的主键id
            dirPath = "/" + department.getId();
        }else {
            // 否则他就有上级部门，那么我们需要查询到上级部门的对象，获取到上级部门的dirpath，拼接我自己的id，就是我的dirpath
            DePartment parentDept = dePartmentMapper.loadById(department.getParent().getId());
            if(parentDept != null){
                dirPath = parentDept.getDirPath() + "/" + department.getId();
            }
        }
        department.setDirPath(dirPath);
        dePartmentMapper.update(department);
    }






    @Override
    public List<DePartment> tree() {



        // 1.一次性查询出全部数据
        List<DePartment> depts = dePartmentMapper.loadAll();

        // 2.创建一个集合对象，用于放最终排列完成的树数据
        List<DePartment> deptTree = new ArrayList<DePartment>();

        // 3.将集合数据换转为map，消除时间复杂度问题
        // Map<Long, Department> deptMap = deptTree.stream().collect(Collectors.toMap(Department::getId, item -> item));
        HashMap<Long, DePartment> deptMap = new HashMap<>();
        for (DePartment department : depts) {
            deptMap.put(department.getId(), department);
        }

        // 4.从所有数据中找到一级数据
        for (DePartment deptTmp : depts) {
            // 如果数据没有父级Id，那么就是一级数据
            if(deptTmp.getParentId() == null){
                deptTree.add(deptTmp);
            }else {
                // 从map集合中获取到父对象，将自己添加到父对象的children集合中
                DePartment department = deptMap.get(deptTmp.getParentId());
                if (department != null){
                    department.getChildren().add(deptTmp);
                }
            }
        }

        return deptTree;
    }

}